var apiScript={}

var SYNC_DES=function(text,key){
    key=key||'20180313';
    return CryptoJS.enc.Base64.stringify(CryptoJS.DES.encrypt(CryptoJS.enc.Utf8.parse(text), CryptoJS.enc.Utf8.parse(key),{ iv: CryptoJS.enc.Utf8.parse(key),mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7 }).ciphertext)
}

Date.prototype.format = function (format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}

function apiData(obj){
    var result={};
    req = JSON.parse(obj.requestParameter);
    rsp = JSON.parse(obj.returnParam)
    if (obj.apiKey == 'YH01020013040' && rsp.code == '0' && rsp.data.result == '0') {
        result= {phone: rsp.data.phone, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020013020' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_timelength: rsp.data.result, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH02010011001' && rsp.code == '0' && rsp.data.operator && (((rsp.data.operator == '1' || rsp.data.operator == '3') && rsp.data.result == 'T') || (rsp.data.operator == '2' && rsp.data.result == '00'))) {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH02020011003' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_status: rsp.data.result, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020012040' && rsp.code == '0' && rsp.data.isBlack == '1') {
        result= {phone: rsp.data.phone, phone_black_type: 1, phone_black_timelength: rsp.data.createTime};
    }
    if (obj.apiKey == 'YH01020012040' && rsp.code == '0' && rsp.data.isBlack == '0') {
        result= {phone: rsp.data.phone, phone_black_type: 0, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010012000' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010012001' && rsp.code == '0' && rsp.data.result == '1') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010012010' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010012011' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020012030' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_timelength: rsp.data.result, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020012020' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_status: rsp.data.result, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020012050' && rsp.code == '0' && rsp.data.message) {
        result= {phone: rsp.data.phone, phone_month_money: rsp.data.message, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010011000' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020011030' && rsp.code == '0' && rsp.data.resultCode == '0') {
        result= {phone: req.phone, phone_timelength: rsp.data.resultValue, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020011020' && rsp.code == '0' && rsp.data.resultCode == '0') {
        result= {phone: req.phone, phone_status: rsp.data.state, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010011010' && rsp.code == '0' && rsp.data.resultCode == '0') {
        result= {phone: req.phone, name: req.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020011090' && rsp.code == '0' && rsp.data.resultCode == '0') {
        result= {phone: req.phone, phone_month_money: rsp.data.resultValue, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020011070' && rsp.code == '0' && rsp.data.resultValue == '0') {
        result= {phone:rsp.data.phone,phone_black_type:1,phone_black_level:rsp.data.resultValue};
    }
    if (obj.apiKey == 'YH01020011070' && rsp.code == '0' && rsp.data.resultValue != '0') {
        result= {phone: rsp.data.phone, phone_black_type: 1, phone_black_level: rsp.data.resultValue, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01020013010' && rsp.code == '0' && rsp.data.result && rsp.data.result != '0') {
        result= {phone: rsp.data.phone, phone_status: rsp.data.result, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'YH01010013000' && rsp.code == '0' && rsp.data.checkResult == '00') {
        result= {idcard: rsp.data.identityCard, name: rsp.data.name, phone: req.phone, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    // if (obj.apiKey == 'YH01010031000' && rsp.code == '0' && rsp.data.resultCode == '1200') {
    //     result= {bankCard: rsp.data.bankCard, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'yh'};
    //     bulidBankcard(result);
    //     bulidIdcard(result);
    //     bulidPhone(result);
    // }
    if (obj.apiKey == 'YH01010031010' && rsp.code == '0' && rsp.data.resultCode == '1200') {
        result= {
            bankCard: rsp.data.bankCard,
            idcard: rsp.data.identityCard,
            name: rsp.data.name,
            phone: req.phone,
            source: 'yh'
        };
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01010' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01011' && rsp.code == '0' && rsp.data.result == '1') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01020' && rsp.code == '0' && rsp.data.resultCode == '0') {
        result= {phone: req.phone, name: req.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01030' && rsp.code == '0' && rsp.data.result) {
        result= {phone: req.phone, phone_status: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01050' && rsp.code == '0' && rsp.data.resultCode == '0') {
        result= {phone: req.phone, phone_month_money: rsp.data.resultValue, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01100' && rsp.code == '0' && rsp.data.resultValue == '0') {
        result= {phone: rsp.data.phone, phone_black_type: 0};
    }
    if (obj.apiKey == 'A01100' && rsp.code == '0' && rsp.data.resultValue != '0') {
        result= {phone: rsp.data.phone, phone_black_type: 1, phone_black_level: rsp.data.resultValue, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02010' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02011' && rsp.code == '0' && rsp.data.result == '1') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02020' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02021' && rsp.code == '0' && rsp.data.result == 'T') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02030' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_status: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02040' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_timelength: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02050' && rsp.code == '0' && rsp.data.message) {
        result= {phone: rsp.data.phone, phone_month_money: rsp.data.message, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A02060' && rsp.code == '0' && rsp.data.isBlack == '1') {
        result= {phone: rsp.data.phone, phone_black_type: 1, phone_black_timelength: rsp.data.createTime};
    }
    if (obj.apiKey == 'A02060' && rsp.code == '0' && rsp.data.isBlack == '0') {
        result= {phone: rsp.data.phone, phone_black_type: 0, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A03010' && rsp.code == '0' && rsp.data.checkResult == '00') {
        result= {idcard: rsp.data.identityCard, name: rsp.data.name, phone: req.phone, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A03011' && rsp.code == '0' && rsp.data.checkResult == '00') {
        result= {idcard: rsp.data.identityCard, name: rsp.data.name, phone: req.phone, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A03020' && rsp.code == '0' && rsp.data.result && rsp.data.result != '0') {
        result= {phone: req.phone, phone_status: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A03030' && rsp.code == '0' && rsp.data.result) {
        result= {phone: req.phone, phone_timelength: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A03050' && rsp.code == '0' && rsp.data.result == '0') {
        result= {phone: rsp.data.phone, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A03051' && rsp.code == '0' && rsp.data.result == '1') {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A04010' && rsp.code == '0' && (((rsp.data.operator == '1' || rsp.data.operator == '3') && rsp.data.result == 'T') || (rsp.data.operator == '2' && rsp.data.result == '00'))) {
        result= {phone: rsp.data.phone, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A04020' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_status: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    // if (obj.apiKey == 'C03011' && rsp.code == '0' && rsp.data.resultCode == '1200') {
    //     result= {bankCard: rsp.data.bankCard, idcard: rsp.data.identityCard, name: rsp.data.name, source: 'xh'};
    //     bulidBankcard(result);
    //     bulidIdcard(result);
    //     bulidPhone(result);
    // }
    if (obj.apiKey == 'C04011' && rsp.code == '0' && rsp.data.resultCode == '1200') {
        result= {
            bankCard: rsp.data.bankCard,
            idcard: rsp.data.identityCard,
            name: rsp.data.name,
            phone: req.phone,
            source: 'xh'
        };
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }
    if (obj.apiKey == 'A01031' && rsp.code == '0' && rsp.data.result) {
        result= {phone: rsp.data.phone, phone_status: rsp.data.result, source: 'xh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }

    if(result&&result.phone&&!isNaN(result.phone)) {
        result['apiKey']=obj.apiKey;
        result['data_use']=obj.userName;
        result['phone_two']=result.phone.substring(0,2);
        result['phone_three']=result.phone.substring(0,3);
        if(obj.localInvok){
            result['data_source']=obj.localInvok;
        }
        var zuizhong = {}
        var content = [];
        for (key in result) {
            if(result[key] instanceof Date){
                zuizhong[key]=result[key].format("yyyy-MM-dd hh:mm:ss");
            }else{

                content.push(key + "_" + result[key]);
            }

        }
        zuizhong.phone = result.phone;

        zuizhong.content = content.join(" ")
        return zuizhong;
    }
}

function logData(array){
    var results=[];
    for(var i=0;i<array.length;i++){
        var obj=array[i]
        var result={};
        req = JSON.parse(obj.requestParameter);
        rsp = JSON.parse(obj.returnParam);
        var content=[];
        var query={};
        for (key in obj) {
            if(key!='_id'&&key!='_class'&&key!='visitDate'&&key!='requestParameter'&&key!='status'&&key!='price'&&key!='returnParam'&&key!='consumeTime'&&key!='is_charge'&&key!='userMoney'){
                query[key]=obj[key];
            }
        }
        for (key in req) {
            if(key!='ip'&&key!='format'&&key!='isCharge'&&key!='userMoney'&&key!='photo'){
                query[key]=req[key];
            }
        }

        if(rsp.data){
            for (key in rsp.data) {
                if(!rsp.data.length&&typeof rsp.data[key]=='string'&&rsp.data[key].length<40){
                    query[key]=rsp.data[key];
                }
            }
        }
        for (key in query) {
            content.push(query[key]);
        }

        result['data']=obj;
        result['content']=content.join(" ");
        results.push(result)

    }
    return results;

}

function apiDataA03021(obj){
    var result={};
    req = JSON.parse(obj.requestParameter);
    rsp = JSON.parse(obj.returnParam)
    if (obj.apiKey == 'A03021' && rsp.code == '0' && rsp.data.result == '0') {
        result= {phone: rsp.data.phone, name: rsp.data.name, source: 'yh'};
        bulidBankcard(result);
        bulidIdcard(result);
        bulidPhone(result);
    }


    if(result&&result.phone) {
        result['apiKey']=obj.apiKey;
        result['data_use']=obj.userName;
        result['phone_two']=result.phone.substring(0,2);
        result['phone_three']=result.phone.substring(0,3);
        if(obj.localInvok){
            result['data_source']=obj.localInvok;
        }
        var zuizhong = {}
        var content = [];
        for (key in result) {
            if(result[key] instanceof Date){
                zuizhong[key]=result[key].format("yyyy-MM-dd hh:mm:ss");
            }else{

                content.push(key + "_" + result[key]);
            }

        }
        zuizhong.phone = result.phone;

        zuizhong.content = content.join(" ")
        return zuizhong;
    }
}